import sys
import csv
# import internetdata

# http://docs.python.org/tutorial/stdlib.html#command-line-arguments
# location of file with case numbers
file = sys.argv[1]

# http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files
# read in file of case numbers and make a set
f = open(file, 'r')
fsnumbers = map(lambda n: n.replace('\n',''),f)
fscasenumbers = set(fsnumbers)

# http://docs.python.org/library/csv.html
caseReader = csv.reader(open('F  S 2010 data from RIS.csv', 'r'), dialect='excel')
caseWriter = csv.writer(open('newCaseList'+sys.argv[1]+'.csv', 'w'), dialect='excel')

caseDict = {}

# turn caseReader into dictionary
for row in caseReader:
    caseDict[row.pop(0)] = row

cases = caseDict.keys()

# totalcases = set(caseDict.keys())
# f & totalcases

# for each case in the list of cases check if case is in set fscasenumbers
for case in cases:
    # some cases seem to be in the smaller list but not in the larger one?! error...
    try:
        if case in fscasenumbers: 
            # TODO: use internetdata.main(case) to return a string of the names
            # of defendants and put this string into cell in this csv row
            caseWriter.writerow([case]+caseDict[case])
        else:
            pass
    except: pass
